import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
import logging
from Book.Book import Book

# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

class bookDataSpider(object):
    def GetHtml(self, PageNum: int):
        logging.info("正在爬取中国图书网")
        url = 'https://www.bookschina.com/book_find2/default.aspx?stp=%u513f%u7ae5&scate=0&f=1&sort=0&asc=0&sh=0&so=1&p=3&pb={}'.format(PageNum)
        r = requests.get(url)
        logging.info("爬取成功")
        return r.text

    def ParseSinglePage(self, page_num):
        book_list = []
        bsObj = BeautifulSoup(self.GetHtml(page_num), 'html.parser')
        BookList = bsObj.find_all('div', {'class': 'bookList'})

        for div in BookList:
            ul = div.find('ul')
            if ul:
                li_list = ul.find_all('li')
                for li in li_list:
                    book = Book()
                    fir_div = li.find('div', {'class': 'cover'})
                    if fir_div:
                        a = fir_div.find('a')
                        img = fir_div.find('img')
                        if a:
                            title = str(a.get('title'))
                            book.name = title
                        if img:
                            img_src = "https:" + str(img.get('data-original'))
                            book.url = img_src
                            book_list.append(book)
                    notice = li.find('div', {'class': 'infor'})
                    if notice:
                        a = notice.find('a', {'class': 'author'})
                        publish_time = notice.find('span', {'class': 'pulishTiem'})
                        publisher = notice.find('a', {'class': 'publisher'})
                        sell_price = notice.find('span', {'class': 'sellPrice'})
                        priceTit=notice.find('del', {'class': ''})
                        topic = notice.find('p', {'class': 'recoLagu'})
                        if a:
                            book.author = a.text
                        if publish_time:
                            book.publish_time = publish_time.text.replace('/', '')
                        if publisher:
                            book.publisher = publisher.text
                        if sell_price:
                            price_text = sell_price.text.replace('¥', '')  # 去除货币符号
                            book.sell_price = float(price_text)
                        if topic:
                            book.topic = topic.text
                        if priceTit:
                            price_text = priceTit.text.replace('¥', '')  # 去除货币符号
                            book.priceTit = float(price_text)
        logging.info("已成功爬取第{}页".format(page_num))
        return book_list

    def ParseHtml(self, num: int):
        book_list = []
        with ThreadPoolExecutor() as executor:
            results = executor.map(self.ParseSinglePage, range(1, num+1))
            for result in results:
                book_list.extend(result)
        return book_list

if __name__ == '__main__':
    s = bookDataSpider()
    a = s.ParseHtml(1)
